package com.allen.controller;

import com.allen.bean.Result;
import com.allen.bean.User;
import com.allen.service.UserService;
import com.allen.util.Jwtutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController    //相当于@Controller+@RequestBody
@RequestMapping("/user")
@CrossOrigin(origins ="*")
public class UserController {

    @RequestMapping("hello")
    public String hello(){
        return "hello";
    }

    //调用service层
    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param user 参数封装
     * @return Result
     */
    @RequestMapping("reg")
    public Result regist(User user){
        System.out.println("成功进入controller");
        return userService.regist(user);
    }

    /**
     * 登录
     * @param user 参数封装
     * @return Result
     */
    @RequestMapping("login")
    public Result login(User user){

        return userService.login(user);
    }

    @RequestMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        //验证token合法性，即是否有效
        String token=request.getHeader("token");
        return Jwtutil.checkToken(token);
    }

}
